package org.wisdom.data.jpa.filter;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

/**
 * @author 张智研
 * @date 2023/04/23 16:42
 **/
@Component
@Aspect
public class DataFilterAdvice {

    @PersistenceContext
    private EntityManager entityManager;

    @Around("@annotation(org.wisdom.annotation.EnableDataFilter)")
    public Object doProcess(ProceedingJoinPoint joinPoint) throws Throwable {
        try{
            // 获取登陆用户角色
//            Filter filter = entityManager.unwrap(Session.class).enableFilter("filterInCompanyId");
//            List<String> names = new ArrayList<>();
//            Character[] b = {'a','b','C'};
//            names.add("a");
//            names.add("b");
//            names.add("c");
//            filter.setParameter("filterCode", names);
            return joinPoint.proceed();
        }catch (Throwable ex){
            ex.printStackTrace();
            throw ex;
        }finally {
            entityManager.unwrap(Session.class).disableFilter("filterByHotelCode");
        }
    }
}